<?php
class Ability extends AdminController {
    public function __construct() {
        parent::__construct();
    }
    
    public function index() {
        $this->_data["loadPage"]="ability/index_view";
        $this->_data["titlePage"]="Quản lý khả năng";
        $this->_data["mess_succ"]=$this->session->flashdata("flash_mess");
        
        $this->session->unset_userdata("sess_search_provider");
        $this->session->unset_userdata("sess_search_title");
        $this->session->unset_userdata("sess_search_from");
        $this->session->unset_userdata("sess_search_to");
        
        $this->load->model("mability");
        $this->load->library("pagination");
        $config=array(
            "base_url"=>base_url()."admin_combo/ability/index/",
            "total_rows"=>$this->mability->countAll(),
            "per_page"=>$this->_data["perpage"],
            "uri_segment"=>4
        );
        $start=$this->uri->segment(4)==""?0:$this->uri->segment(4);
        $this->pagination->initialize($config);
        if($this->mability->listAll($config["per_page"], $start) == false)
            $this->_data["info"] = "empty";
        else
            $this->_data["info"]=$this->mability->listAll($config["per_page"], $start);
        
        $this->load->view($this->_data["path"], $this->_data);
    }
    
    public function search() {
        $this->_data["loadPage"]="ability/search_view";
        $this->_data["titlePage"]="Quản lý khả năng";
        
        $prov=$this->session->userdata("sess_search_provider");
        $title=$this->session->userdata("sess_search_title");
        $from=$this->session->userdata("sess_search_from");
        $to=$this->session->userdata("sess_search_to");
        
        $this->load->model("mprovider");
        $this->load->helper("provider_helper");
        $this->_data["info_provider"]=$this->mprovider->listAll();
        
        $this->load->model("mability");
        $this->load->library("pagination");
        $config=array(
            "base_url"=>base_url()."admin_combo/ability/search/",
            "total_rows"=>$this->mability->countAllResults($prov, $title, $from, $to)["count"],
            "per_page"=>$this->_data["perpage"],
            "uri_segment"=>4
        );
        $start=$this->uri->segment(4)==""?0:$this->uri->segment(4);
        $this->pagination->initialize($config);
        if($this->mability->search($prov, $title, $from, $to, $config["per_page"], $start) == false)
            $this->_data["info"] = "empty";
        else
            $this->_data["info"]=$this->mability->search($prov, $title, $from, $to, $config["per_page"], $start);
        
        $this->load->view($this->_data["path"], $this->_data);
    }
    
    public function searchdata() {
        if(isset($_POST["btnSearchAbility"])) {
            $sess=array(
                "sess_search_provider"=>$this->input->post("slProvider"),
                "sess_search_title"=>$this->input->post("txtTitle"),
                "sess_search_from"=>$this->input->post("txtFrom"),
                "sess_search_to"=>$this->input->post("txtTo")
            );
            $this->session->set_userdata($sess);
            redirect(base_url()."admin_combo/ability/search");
        }
    }
    
    public function add() {
        $pro_id=$this->uri->segment(4);
        $this->_data["loadPage"]="ability/add_view";
        $this->_data["titlePage"]="Thêm khả năng";
        
        $this->load->model("mprovider");
        $this->load->helper("provider_helper");
        $this->_data["info_provider"]=$this->mprovider->listAll();
        
        $this->load->model("mproduct");
        $this->_data["info_product"]=$this->mproduct->getById($pro_id);
        
        $this->load->helper("form");
        $this->load->library("form_validation");
        $this->form_validation->CI =& $this;
        
        $config=array(
            array(
                "field"=>"txtUnitPrice",
                "label"=>"Đơn Giá",
                "rules"=>"required"
            )
        );
        $this->form_validation->set_rules($config);
        
        //add
        if($this->form_validation->run()) {
            $this->load->model("mability");
            $data=array(
                "prov_id"=>$this->input->post("slProvider"),
                "pro_id"=>$pro_id,
                "ability_unit_price"=>$this->input->post("txtUnitPrice"),
                "ability_note"=>$this->input->post("txtNote")
            );
            $this->mability->insert($data);
            $this->session->set_flashdata("flash_mess", "Thêm khả năng thành công.");
            redirect(base_url()."admin_combo/ability");
        }
        
        $this->load->view($this->_data["path"], $this->_data);
    }
    
    public function edit() {
        $this->_data["loadPage"]="ability/edit_view";
        $this->_data["titlePage"]="Chỉnh sửa khả năng";
        
        //load data
        $id=$this->uri->segment(4);
        $this->load->model("mprovider");
        $this->load->helper("provider_helper");
        $this->_data["info_provider"]=$this->mprovider->listAll();
        
        $this->load->model("mability");
        if($this->mability->getById($id)!=false)
            $this->_data["info_edit"]=$this->mability->getById($id);
        
        //validate and update
        $this->load->helper("form");
        $this->load->library("form_validation");
        $this->form_validation->CI =& $this;
        
        $config=array(
            array(
                "field"=>"txtUnitPrice",
                "label"=>"Đơn Giá",
                "rules"=>"required"
            )
        );
        $this->form_validation->set_rules($config);
        
        //add
        if($this->form_validation->run()) {
            $this->load->model("mability");
            $data=array(
                "prov_id"=>$this->input->post("slProvider"),
                "ability_unit_price"=>$this->input->post("txtUnitPrice"),
                "ability_note"=>$this->input->post("txtNote")
            );
            $this->mability->update($data, $id);
            $this->session->set_flashdata("flash_mess", "Chỉnh sửa khả năng thành công.");
            redirect(base_url()."admin_combo/ability");
        }
        
        $this->load->view($this->_data["path"], $this->_data);
    }
    
    public function del() {
        $id=$this->uri->segment(4);
        $this->load->model("mability");
        $this->mability->del($id);
        $this->session->set_flashdata("flash_mess", "Xóa khả năng thành công.");
        redirect(base_url()."admin_combo/ability/");
    }
}
?>
